using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(SpriteRenderer))]
[RequireComponent(typeof(Collider2D))]
public class WeaponPickup:ItemPickup {
	override protected void Start() {
		base.Start();
		spriteRenderer.sprite=weapon.weaponData.sprite;
	}
	private void SetWeapon(WeaponInstance weapon) {
		this.weapon=weapon;
		if(spriteRenderer!=null) spriteRenderer.sprite=weapon.weaponData.sprite;
	}

	static GameObject pickablePrefab;
	public static WeaponPickup Create(Vector2 position,WeaponInstance weapon) {
		if(pickablePrefab==null) pickablePrefab=Resources.Load<GameObject>("WeaponPickup");
		GameObject gameObject = Instantiate(pickablePrefab,position,Quaternion.identity,Utility.dynamicParent.transform);
		WeaponPickup pickup = gameObject.GetComponentInChildren<WeaponPickup>();
		pickup.SetWeapon(weapon);
		return pickup;
	}
	public static WeaponPickup Create(Vector2 position,WeaponData weapon) {
		int slotCount = 4;
		if(Utility.Chance(0.3f)) slotCount++;
		if(Utility.Chance(0.3f)) slotCount--;
		WeaponInstance _weapon = new WeaponInstance(weapon,slotCount);
		return (Create(position,_weapon));
	}

	public WeaponInstance weapon { get; private set; }
	protected override void HandleInteract() {
		if(!CostPrice(Player.instance)) return;
		if(Player.instance.GiveWeapon(weapon)) Consume();
	}

	public override string GetDescr() { return weapon.descr; }
	public override string GetName() { return weapon.name; }

}
